package com.ptszyxx.popose.module.main.home;

import static java.io.File.separator;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.databinding.ObservableField;
import androidx.lifecycle.ViewModelProviders;
import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.constants.BundleConstant;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ptszyxx.popose.databinding.FragmentHomeWorldDetailBinding;
import com.ptszyxx.popose.module.main.home.vm.HomeWorldDetailVM;
import com.ptszyxx.popose.module.main.user.adapter.UserDynamicAdapter;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.http.data.entity.dynamic.DynamicResult;
import com.ysg.http.data.entity.user.UserResult;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YDialogUtil;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YUserUtil;
import com.ysg.widget.dialog.GiftDialog;
import com.ysg.widget.dialog.entity.GiftListResult;
import com.ysg.widget.dialog.entity.GiftNumResult;

import java.util.List;

public class HomeWorldDetailFragment extends BaseFragment<FragmentHomeWorldDetailBinding, HomeWorldDetailVM> {
    private UserDynamicAdapter adapter;

    public HomeWorldDetailFragment(){
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_home_world_detail;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public HomeWorldDetailVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(HomeWorldDetailVM.class);
    }

    @Override
    public void initParam() {
        super.initParam();
        Bundle bundle = getArguments();
        if (bundle != null){
            viewModel.userId = bundle.getString(BundleConstant.USER_ID);
        }
    }

    @Override
    public void initView() {
        super.initView();
        if (YUserUtil.isMe(viewModel.userId)){
            binding.viewRight.setVisibility(View.VISIBLE);
            binding.viewBottom.setVisibility(View.GONE);
        }
    }

    @Override
    public void initAdapter() {
        super.initAdapter();
        adapter = new UserDynamicAdapter();
        binding.refreshView.initAdapter(adapter);
    }


    @Override
    public void initData() {
        super.initData();
        viewModel.requestUser();
        viewModel.requestList(true);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onRefreshEvent.observe(this, list -> setAdapterData((List<DynamicResult>)list));
        viewModel.uc.onTagEvent.observe(this, o -> setTagUI());
        viewModel.uc.onInfoEvent.observe(this, o -> setInfoUI());
        viewModel.baseUC.onGiftDialogEvent.observe(this, o -> showDialogGift());
    }

    private void setAdapterData(List<DynamicResult> list) {
        binding.refreshView.setAdapterUI(adapter, list, viewModel.isRefresh());
    }

    private void setTagUI(){
        if (viewModel.userEntity.get() != null){
            List<String> list = viewModel.userEntity.get().getBiaoqianList();
            if (YCollectionUtil.isNotEmpty(list)){

            }binding.flowlayout.setData(list, null);
        }
    }
    private void setInfoUI(){
        if (viewModel.userEntity.get() != null){
            //个人信息：拼接
            UserResult userEntity = viewModel.userEntity.get();
            StringBuffer sb = new StringBuffer();
            if (YStringUtil.isNotEmpty(userEntity.getAddress())){
                sb.append(userEntity.getAddress() + separator);
            }
            if (YStringUtil.isNotEmpty(userEntity.getAge() + "") && YStringUtil.noEq(0, userEntity.getAge() + "")){
                sb.append(userEntity.getAge() + getContext().getResources().getString(R.string.year_old) + separator);
            }
            if (YStringUtil.isNotEmpty(userEntity.getShengao())){
                sb.append(userEntity.getShengao() + separator);
            }
            if (YStringUtil.isNotEmpty(userEntity.getZhiyename())){
                sb.append(userEntity.getZhiyename() + separator);
            }
            binding.tvInfo.setText(YStringUtil.clearEnd(sb.toString(), separator));
        }
    }

    /**
     * 礼物
     */
    private void showDialogGift(){
        UserResult entity = viewModel.userEntity.get();
        if (entity != null){
            YDialogUtil.getInstance().showGift(getContext(), viewModel.giftListEntity, viewModel.giftNumResult)
                    .setOnGiftListener(new GiftDialog.OnGiftListener() {
                        @Override
                        public void onRechargeClick() {
                            YActivityUtil.getInstance().jumpMineRecharge(viewModel);
                        }

                        @Override
                        public void onSendClick(GiftListResult.GiftBean giftBean, GiftNumResult.Data giftNumEntity) {
                            viewModel.requestSendGift(giftBean, giftNumEntity,  viewModel.userEntity.get().getTengxuncode(), null,  (gifUrl)->
                                    YDialogUtil.getInstance().showSVGA(getContext(), gifUrl)
                            );
                        }
                    });
        }
    }


}
